﻿CREATE PROCEDURE [dbo].[wikiPages_get_listing]
    @page int = 0,
    @size int = 0,
    @column nvarchar(256),
    @direction int = 0,
    @name nvarchar(256) = null
AS
    SELECT 
        PageId,
        Name,
        CreationDate
    From WikiPages
    Where 
    (@name IS NULL OR WikiPages.Name like '%' + @name + '%')
    ORDER BY 
        CASE WHEN @column = 'Name' AND @direction = 1 THEN WikiPages.Name END DESC,
        CASE WHEN @column = 'Name' AND @direction = 0 THEN WikiPages.Name END ASC,
        CASE WHEN @column = 'CreationDate' AND @direction = 1 THEN  WikiPages.CreationDate END DESC,
        CASE WHEN @column = 'CreationDate' AND @direction = 0 THEN  WikiPages.CreationDate END ASC

    OFFSET ((@page -1)*@size) ROWS 
    FETCH NEXT @size ROWS ONLY;
    
Declare 
@TotalCount int
 SELECT @TotalCount= Count(DISTINCT  WikiPages.PageId)
	FROM  WikiPages
	where 
		(@name is NULL OR  WikiPages.Name like '%'+ @name + '%')

RETURN @TotalCount;
